//
//  TimeHelper.m
//  DeyiLife
//
//  Created by zxin on 15/5/21.
//  Copyright (c) 2015年 Deyi. All rights reserved.
//

#import "TimeHelper.h"

@implementation TimeHelper

+ (NSDateComponents*)intervalSinceDateTimeNow1:(NSTimeInterval)theDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *tempDate = [NSDate dateWithTimeIntervalSince1970:theDate];
    [dateFormatter setDateFormat:@"MM-dd"];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    NSDateComponents *comps  = [calendar components:unitFlags fromDate:tempDate];
    return comps;
}

+ (NSString *)timestampToDate:(NSTimeInterval)theDate {
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:theDate];
    NSTimeInterval timeInterval = [d timeIntervalSinceNow];
    timeInterval = -timeInterval;
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSDate *tempDate = [NSDate dateWithTimeIntervalSince1970:theDate];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *currentDateStr = [dateFormatter stringFromDate:tempDate];
    return currentDateStr;
}


+ (NSString *)timestampToDateTime:(NSTimeInterval)theDate {
    
     NSDate *tempDate = [NSDate dateWithTimeIntervalSince1970:theDate];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *currentDateStr = [dateFormatter stringFromDate:tempDate];
    return currentDateStr;
}


+ (NSString *)timestampHHmmssTime:(NSTimeInterval)theDate
{
    
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:theDate];
    NSTimeInterval timeInterval = [d timeIntervalSinceNow];
    int hour = (int)(timeInterval/3600);
    int minute = (int)(timeInterval - hour*3600)/60;
    int second = timeInterval - hour*3600 - minute*60;
    NSString *dural = [NSString stringWithFormat:@"%d时%d分%d秒", hour, minute,second];
    return dural;
}



+ (NSString *)intervalSinceNow:(NSTimeInterval)theDate {
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:theDate];
    NSTimeInterval timeInterval = [d timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;

    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
        return result;
    } else if((temp = timeInterval / 60) < 60) {
        result = [NSString stringWithFormat:@"%ld分钟前", temp];
        return  result;
    } else if((temp = temp / 60) < 24) {
        result = [NSString stringWithFormat:@"%ld小时前", temp];
        return  result;
    } else if((temp = temp / 24) < 30) {
        //result = [NSString stringWithFormat:@"%ld天前",temp];
        if (temp == 1) {
            int hour= (int)[self intervalSinceDateTimeNow1:theDate].hour;
            if (hour < 12) {
                return  @"昨天-早上";
            } else if (hour > 12 && hour < 16) {
                return  @"昨天-下午";
            } else {
                return  @"昨天-晚上";
            }
        } else if(temp == 2) {
            int hour= (int)[self intervalSinceDateTimeNow1:theDate].hour;
            if (hour < 12)
            {
                return  @"前天-早上";
            }
            else if (hour > 12 && hour < 16)
            {
                return  @"前天-下午";
            } else {
                return  @"前天-晚上";
            }
        } else {
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            NSDate *tempDate = [NSDate dateWithTimeIntervalSince1970:theDate];
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
            NSString *currentDateStr = [dateFormatter stringFromDate:tempDate];
            return currentDateStr;
        }
    }
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSDate *tempDate = [NSDate dateWithTimeIntervalSince1970:theDate];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *currentDateStr = [dateFormatter stringFromDate:tempDate];
    return currentDateStr;
}

+ (NSString *)currentTimeStamp {
    NSTimeInterval inter = [[NSDate date] timeIntervalSince1970];
    NSInteger start = (NSInteger)inter;
    return [NSString stringWithFormat:@"%ld", (long)start];
}

@end
